/*
Copyright 2005-2015 CP Fricano - All Rights Reserved

This software and source code is licensed under the Apache
License, Version 2.0. You may not use this software or source
code except in compliance with the License.  You may obtain a
copy of the License at: http://www.apache.org/licenses/LICENSE-2.0.
Use and distributed under this License is on an "as is" basis,
without expressed or implied warranties or conditions of any kind.
See the License for the specific language governing permissions and
limitations.
 */

package com.fricano.java42.swing.dings.base;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.io.Serializable;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class J42JCompoundPanel<T extends JComponent, U extends JComponent> extends JPanel implements J42JCompoundComponent<U> {

    private static final long                     serialVersionUID = 1L;

    private final J42JCompoundPanel_Manager<T, U> manager          = new J42JCompoundPanel_Manager<>(this);
    private T                                     comp1            = (T) new JButton("T");
    private U                                     comp2            = (U) new JButton("U");

    public static class J42JCompoundPanel_Manager<T extends JComponent, U extends JComponent> implements Serializable {

        final private J42JCompoundPanel<T, U> i;

        public J42JCompoundPanel_Manager(final J42JCompoundPanel<T, U> instance) {
            i = instance;
        }

        public T getComponent1() {
            return i.comp1;
        }

        public U getComponent2() {
            return i.comp2;
        }

        public void setComponent1(final T comp1) {
            i.remove(i.comp1);
            i.comp1 = comp1;
            i.add(i.comp1);
        }

        public void setComponent2(final U comp2) {
            i.remove(i.comp2);
            i.comp2 = comp2;
            i.add(i.comp2);
        }

        public void testIt() {
            final JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

            f.getContentPane().setLayout(new FlowLayout());
            f.getContentPane().add(i);

            f.setSize(300, 200);
            f.setLocationRelativeTo(null);
            f.setVisible(true);

        }

    }

    public J42JCompoundPanel_Manager<T, U> getManager_J42JCompoundPanel_Manager() {
        return manager;
    }

    public J42JCompoundPanel() {
        setLayout(new GridLayout(1, 0));
        setLayout(new FlowLayout());
        add(comp1);
        add(comp2);

        //setFont(new Font("Tahoma", Font.PLAIN, 8));

        //        setBorder(BorderFactory.createTitledBorder(label));
        //        setColumns(8);
        //        setSize(100, 30);
        //        setLayout(new GridBagLayout());
        //        //add(new JLabel(label));
        //        setText(text);
        //        System.out.println(getLayout());
    }

    @Override
    public U getJComponent() {
        return comp2;
    }

}
